package com.taobao.txc.springCloud.provide1;


import com.alibaba.druid.pool.DruidDataSource;
import com.taobao.txc.datasource.cobar.TxcDataSource;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.boot.autoconfigure.condition.ConditionalOnBean;
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
public class Config {


    @Bean("primaryDataSource")
    @Qualifier("primaryDataSource")
    @ConfigurationProperties(prefix = "spring.datasource.primary")
    public DruidDataSource primaryDataSource() {
        return new DruidDataSource();
    }

    @Bean(name = "primaryDataSourceProxy")
    @Qualifier("primaryDataSourceProxy")
    public TxcDataSource primaryDataSourceProxy(@Qualifier("primaryDataSource") DruidDataSource primaryDataSource) {
        return new TxcDataSource(primaryDataSource);
    }

    @Bean(name = "primaryJdbcTemplate")
    @Qualifier("primaryJdbcTemplate")
    @ConditionalOnBean(TxcDataSource.class)
    public JdbcTemplate primaryJdbcTemplate(@Qualifier("primaryDataSourceProxy") TxcDataSource primaryDataSourceProxy) {
        return new JdbcTemplate(primaryDataSourceProxy);
    }


    //    @Bean(name = "primaryJdbcTemplate")
//    public JdbcTemplate primaryJdbcTemplate(
//            @Qualifier("primaryDataSource") javax.sql.DataSource dataSource) {
//        return new JdbcTemplate(dataSource);
//    }

    //定义声明式事务，要想让事务annotation感知的话，要在这里定义一下
//    @Bean(name = "txcScanner")
//    @ConfigurationProperties(prefix="aluser")
//    public TxcTransactionScaner txcTransactionScaner()
//    {
//        //xxxx填写txc的逻辑组名
//        return  new TxcTransactionScaner("xxx");
//    }

}
